package com.hyt.it.ogt.ks.controller;


import com.hyt.common.apiInfor.ApiResponse;
import com.hyt.it.ogt.ks.enums.BizStatusCode;
import com.hyt.it.ogt.ks.enums.log.ApplyCheckState;
import com.hyt.it.ogt.ks.enums.log.LogEventType;
import com.hyt.it.ogt.ks.enums.log.WebsocketMsgSendState;
import com.hyt.it.ogt.ks.exception.KsException;
import com.hyt.it.ogt.ks.model.param.ApplyCheckResultMsgConfirmParam;
import com.hyt.it.ogt.ks.service.IApplyCheckMsgService;
import com.hyt.it.ogt.ks.service.ICandidateLogOtherService;
import com.hyt.it.ogt.opertaion.track.model.OtherLogVO;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 审核消息发送记录表 前端控制器
 * </p>
 *
 * @author zhoumiao
 * @since 2022-12-19
 */
@RestController
@Slf4j
@RequestMapping("/ks/apply-check-msg")
@Api(tags = "35.审核消息接口")
@ApiSort(35)
public class ApplyCheckMsgController {

    @Resource
    private IApplyCheckMsgService applyCheckMsgService;

    @ApiOperation(value = "35.1 考生收到异常管理审核结果消息确认接口")
    @ApiOperationSort(1)
    @PutMapping("/applyCheckResultMsgConfirm")
    public ApiResponse<Boolean> applyCheckResultMsgConfirm(@Validated @RequestBody ApplyCheckResultMsgConfirmParam applyCheckResultMsgConfirmParam
    ) {
        try {
            log.info("考生收到异常管理审核结果消息确认接口：{}", applyCheckResultMsgConfirmParam);
            return ApiResponse.<Boolean>builder()
                    .code(BizStatusCode.KS_SUCCESS_OK.getCode())
                    .data(applyCheckMsgService.updateMsgSendState(applyCheckResultMsgConfirmParam.getExamId(),
                            applyCheckResultMsgConfirmParam.getCandidateId(), LogEventType.OPERATOR_LOG.getCode(),
                            applyCheckResultMsgConfirmParam.getOperateType(),
                            convertState(applyCheckResultMsgConfirmParam.getMessageType()),
                            WebsocketMsgSendState.SEND_SUCCESS))
                    .build();
        } catch (KsException e) {
            log.error("考生收到异常管理审核结果消息确认接口报业务错误：", e);
            return ApiResponse.<Boolean>builder().code(e.getErrCode()).data(false).build();
        } catch (Exception e) {
            log.error("考生收到异常管理审核结果消息确认接口报内部错误：", e);
            return ApiResponse.<Boolean>builder().code(BizStatusCode.CANDIDATE_WEBSOCKET_MSG_CONFIRM_FAIL.getCode()).data(false).build();
        }
    }

    private int convertState(String messageType) {
        switch (messageType) {
            case "faceVerifyNotPass":
                return ApplyCheckState.NO_PASSED.getCode();
            case "deviceVerifyNotPass":
                return ApplyCheckState.NO_PASSED.getCode();
            case "deviceVerifyPass":
                return ApplyCheckState.PASSED.getCode();
            case "faceVerifyPass":
                return ApplyCheckState.PASSED.getCode();
            case "resetAnswerTime":
                return ApplyCheckState.PASSED.getCode();
        }
        return -1;
    }

}
